/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.x3.purchasing.quotation;

import com.x3.purchasing.common.ClassConnection;
import com.x3.purchasing.inquery.Inquery;
import com.x3.purchasing.vendor.Vendor;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author kris
 */
public class QuotationDAOImpl implements QuotationDAO {

    public QuotationDAOImpl() {
        ClassConnection.getTransactionProxyFactoryBean().setTarget(this);
    }

    public void insert(Quotation quotation) throws Exception {
        String sql = "INSERT INTO quotation(tanggal,penawaran,vendor_id,inquery_id,nama_file) VALUES(?,?,?,?,?)";
        PreparedStatement ps = ClassConnection.getJdbc().getDataSource().getConnection().prepareStatement(sql);
        ps.setDate(1, new java.sql.Date(quotation.getTanggal().getTime()));
        ps.setBinaryStream(2, quotation.getPenawaran(), 1024 * 10000);
        ps.setInt(3, quotation.getVendor().getId());
        ps.setInt(4, quotation.getInquery().getId());
        ps.setString(5, quotation.getNamaFile());
        ps.execute();
        ps.close();
    }

    public void update(Quotation quotation) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void delete(int id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Quotation getByAccount(int id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Quotation get(int id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Quotation> gets() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Quotation> getByInquery(int inqueryId) throws Exception {
        List<Quotation> quotations = new ArrayList<Quotation>();
        String sql = "SELECT *FROM quotation q "
                + " INNER JOIN inquery i ON i.id = q.inquery_id "
                + " INNER JOIN vendor v ON v.id = q.vendor_id "
                + " WHERE i.id = ?";
        PreparedStatement ps = ClassConnection.getJdbc().getDataSource().getConnection().prepareStatement(sql);
        ps.setInt(1, inqueryId);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            Inquery inquery = new Inquery();
            inquery.setJudul(rs.getString("judul"));
            inquery.setKeterangan(rs.getString("keterangan"));
            inquery.setTanggalBuka(rs.getDate("tanggal_buka"));
            inquery.setTanggalTutup(rs.getDate("tanggal_tutup"));

            Vendor vendor = new Vendor();
            vendor.setNama(rs.getString("nama"));
            vendor.setAlamat(rs.getString("alamat"));
            vendor.setEmail(rs.getString("email"));
            vendor.setKontak(rs.getString("kontak"));

            Quotation quotation = new Quotation();
            quotation.setInquery(inquery);
            quotation.setVendor(vendor);
            quotation.setTanggal(rs.getDate("tanggal"));
            quotation.setNamaFile(rs.getString("nama_file"));
            quotation.setPenawaran(rs.getBinaryStream("penawaran"));

            quotations.add(quotation);
        }
        return quotations;
    }
}
